#region License, Terms and Author(s)
//
// Schnell - Wiki widgets
// Copyright (c) 2007 Atif Aziz. All rights reserved.
//
//  Author(s):
//      Atif Aziz, http://www.raboof.com
//
// This library is free software; you can redistribute it and/or modify it 
// under the terms of the GNU Lesser General Public License as published by 
// the Free Software Foundation; either version 2.1 of the License, or (at 
// your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, 
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace WikiPad
{
    #region Imports

    using System;
    using System.Windows.Forms;

    #endregion

    internal sealed class ControlFocusWatchdog
    {
        private Control _lastEntered;
        private EventHandler _onEnter;

        public void Watch(Control control)
        {
           control.Enter += OnEnter;
        }

        public void Watch(params Control[] controls)
        {
            Array.ForEach(controls, LaxWatch);
        }

        public Control LastEntered
        {
            get { return _lastEntered; }
        }

        public Control ActiveControl
        {
            get { return LastEntered == null || !LastEntered.Focused ? null : LastEntered; }
        }

        public EventHandler OnEnter
        {
            get
            {
                if (_onEnter == null)
                {
                    _onEnter = delegate(object sender, EventArgs e) {
                       _lastEntered = (Control) sender;
                   };
                }

                return _onEnter;
            }
        }

        private void LaxWatch(Control control)
        {
            if (control == null)
                return;

            Watch(control);
        }
    }
}
